/*
 * @Description: 
 * @Author: Liu Kunpeng
 * @Date: 2021-03-12 19:14:51
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { getUserInfoApi } from '@/api/index'
Vue.use(Vuex)

export default new Vuex.Store({
  // 全局状态
  state: {
    routes: [],
    userinfo:{}
  },
  // 改变state里面值的方法,同步执行
  mutations: {
    InitRoutes(state,data){
      state.routes = data;
    },
    SET_USERINFO(state,data){
        state.userinfo = data; 
    }
  },
  // 异步执行
  actions: {
    getUserInfo(context){
      return new Promise((resolve, reject) => {
        getUserInfoApi().then(response => {
          const data = response.data.user
          
          if (!data) {
            Message.error('获取用户信息错误，请重新登录')
            router.push('/login')
            return reject('获取用户信息错误，请重新登录')
          }
          
          context.commit('SET_USERINFO', data)
          // context.commit('SET_ROLE', role)
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  modules: {

  }
})
